<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" ></meta>
    <title>详细内容</title>
    <link rel="stylesheet" href="${frames}/css/lib/bootstrap.min.css" type="text/css" type="text/css">
    <link rel="stylesheet" href="${frames}/css/lib/bootstrap-datetimepicker.min.css" type="text/css">
    <link rel="stylesheet" href="${newframe}/css/base/base.css" type="text/css">
    <link rel="stylesheet" href="${newframe}/css/common.css" type="text/css">
    <link rel="stylesheet" href="${frames}/css/picker.css" type="text/css">
    <link rel="stylesheet" href="${newframe}/css/select2.min.css" type="text/css">
    <link rel="stylesheet" href="${newframe}/css/beautify-time.css" type="text/css">
    <!--[if IE 8]>
    <link rel="stylesheet" href="${newframe}/css/base/IE8.css" type="text/css"/>
    <![endif]-->
    <style>
        .select2-container {
            width: 100% !important;
        }
    </style>
    <%@ include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
    <script src="${frames}/laydate/laydate.dev.js"></script>
    <script src="${assets}/bootstrapValidator/js/bootstrapValidator.js"></script>
    <script src="${assets}/bootstrapValidator/js/language/zh_CN.js"></script>
    <script src="${newframe}/js/select2.full.zh.js"></script>
    <script>
        $(document).ready(function () {
            $('#myForm').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {

                    state: {
                        validators: {
                            notEmpty: true
                        }
                    },'hlDoctorEntity.id': {
                        validators: {
                            notEmpty: true
                        }
                    },'patientEntity.id': {
                        validators: {
                            notEmpty: true
                        }
                    }
                }
            }).on('success.form.bv', function (e) {
                e.preventDefault();
                var _url="";
                if($("#id").val() == null || $("#id").val() == ''){
                    _url =  '${URL_DOCPPAPPLICATION_SAVE}';
                }else{
                    _url =  '${URL_DOCPPAPPLICATION_UPDATE}';
                }
                $.ajax({
                    url: _url,
                    type: 'post',
                    data: $('#myForm').serialize(),
                    async: false, //默认为true 异步
                    error: function () {
                        alert('error');
                    },
                    success: function (data) {
                        // debugger;
                        console.log(data);
                        data = JSON.parse(data);
                        if(!data.success){
                            alert(data.msg);
                        }
                        closeFrame();
                    }
                });
            });
            initDocktor("doctorid");

            initPatient("patientidcard");

            if(${entity.patientEntity.id != null}){
                $("#patientidcard").html('<option value="${entity.patientEntity.id}">${entity.patientEntity.realname}</option>');
            }

            if(${entity.hlDoctorEntity.id != null}){
                $("#doctorid").html('<option value="${entity.hlDoctorEntity.id}">${entity.hlDoctorEntity.drName}</option>');
            }

        });

        //关闭并刷新
        function closeFrame() {
            window.parent.closeModal();
        }
        //病人下拉
        function formatRepo(repo) {
            if (repo.loading) return repo.text;
            var markup = "<div class='select2-result-repository clearfix'>" +
                "<div class='select2-result-repository__title'>" + repo.text +"</div><p>"+ repo.idcard +"</p></div>";
            return markup;
        }
        function formatRepoSelection(repo) {
                var markup = "<div class='select2-result-repository clearfix'>" +
                        "<div class='select2-result-repository__title'>" + repo.text +"</div></div>";
                return markup;
        }
        //医生下拉
        function formatRepoDoc(repo) {
            if (repo.loading) {
                return repo.text;
            }
            var markup = "<div class='select2-result-repository clearfix'>" +
                    "<div class='select2-result-repository__title'>" + repo.text +"</div></div>";
            return markup;
        }
        function formatRepoSelectionDoc(repo) {
            return repo.text;
        }
        function initPatient(id){
            $("#"+id).select2({
                placeholder: '---请先选择病人---',
                ajax: {
                    url: '${URL_PPPATIENTBASE_LIST_BY_SEARCH}',
                    dataType: 'json',
                    type: "post",
                    delay: 250,
                    data: function (params) {
                        var data=new Object();
                        data["iDisplayLength"]=10;
                        if(params.page==undefined){
                            data["iDisplayStart"]=0;
                        }else {
                            data["iDisplayStart"]= (params.page-1)*10;
                        }
                        if(params.term!=undefined){
                            if(/^\d+$/.test(params.term) || /^\d+(x||X)$/.test(params.term)){
                                data["idcard"] = params.term;
                            }else{
                                data["realname"]= params.term;
                            }
                        }
                        console.log(data);
                        return data;

                        return {
                            q: term
                        };
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.dataList,
                            pagination: {
                                more: (params.page * 10) < data.msg
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0,
                templateResult: formatRepo, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
            });
        }
        function initDocktor(id){
            $("#"+id).select2({
                placeholder: '---请先选择医生---',
                ajax: {
                    url: '${URL_HL_DOCTOR_SEAR}',
                    dataType: 'json',
                    type: "post",
                    delay: 250,
                    data: function (params) {
                        var data=new Object();
                        data["iDisplayLength"]=10;
                        if(params.page==undefined){
                            data["iDisplayStart"]=0;
                        }else {
                            data["iDisplayStart"]= (params.page-1)*10;
                        }
                        if(params.term!=undefined){
                            data["drName"]= params.term;
                        }

//                        console.log(params);
                        return data;
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.dataList,
                            pagination: {
                                more: (params.page * 10) < data.msg
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                minimumInputLength: 0,
                templateResult: formatRepoDoc, // omitted for brevity, see the source of this page
                templateSelection: formatRepoSelectionDoc // omitted for brevity, see the source of this page
            });
        }

    </script>
</head>
<body style="width: 98%">
<form id="myForm" class="form-horizontal" method="post">
    <input type="hidden" id="id" name="id" value="${entity.id}">
    <div class="clearfix">
        <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">医生姓名</label>
            <div class="col-sm-8">
                <select name="hlDoctorEntity.id" id="doctorid"  class="form-control"></select>
            </div>
        </div>
        <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">病人姓名</label>
            <div class="col-sm-8">
                <select name="patientEntity.id" id="patientidcard"  class="form-control"></select>
            </div>
        </div>
        <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">签约状态</label>
            <div class="col-sm-8">
                <select id="concern" name="concern" class="SN">
                    <medi:configOpt name="concern" key="${entity.concern}"/>
                </select>
            </div>
        </div>


    </div>
    <div class="modal-footer" style="text-align: center ">
        <button type="button" class="btn btn-default" data-dismiss="modal" id="closeModalButt" onclick="closeFrame()">
            关闭
        </button>
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>
</body>
</html>